﻿/******************************************************************
 *Company: http://www.xiaomutech.com/
 *fileName : qchessboard.h --- QChessBoard
 *Auth       : yhni (QQ:393320854)
 *Create    : 2022/2/21
 *Description   :
 *Histroy:
 *<Auth>    <Date>        <Ver>        <Content>
 *         2022/2/21
 *******************************************************************/
#ifndef QCHESSBOARD_H
#define QCHESSBOARD_H

#include <QWidget>
#include <QTimer>
#include <QMap>

class QChess;
class QChessBoard : public QWidget
{
    Q_OBJECT
public:
    explicit QChessBoard(QWidget *parent = nullptr);
    ~QChessBoard();

    void selectCamp(bool isRed);
signals:

private:
    void clearChesses();
    void initChesses();
    QChess *getSelected();
    void exchangeChess(QChess *_chess, QChess *_other);
    bool canBeat(QChess *_chess, QChess *_other);
    int  getChessCount(QChess *_chess, QChess *_other);

private:
    int m_xOffSet = 45;
    int m_yOffSet = 45;
    int m_nRadius = 100;

    QColor m_background;
    QColor m_line;

    QMap<quint16, QChess*> m_chesses;

    QPoint  m_prevPos;
    QTimer *m_timer;
    bool    m_showChess;
    bool    m_camp;
protected:
    virtual void adjust();
    void resizeEvent(QResizeEvent *e) override;
    void paintEvent(QPaintEvent *) override;
    void drawPosLine(QPainter *painter, QPoint _pos);
    void drawChess(QPainter *painter, QChess *_chess);
    void drawChessPos(QPainter *painter, QPoint _pos);

    void mousePressEvent(QMouseEvent *e) override;
    void mouseMoveEvent(QMouseEvent *e) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
};

////////////////////////////////////////////////////////////
class QChess {
public:
    typedef enum {
        UnKonw,
        Ju_1, Ma_1, Xiang_1, Shi_1, Jiang, Shuai = Jiang,
        Shi_2, Xiang_2, Ma_2, Ju_2,
        Pao_1, Pao_2,
        Zu_1, Zu_2, Zu_3, Zu_4, Zu_5
    } Type;
    QChess();
    QChess(int _row, int _col, QChess::Type _type);

    quint16 index() const;

    void setRect(int nX, int nY, int nW, int nH);
    QRect rect() const;

    QPoint getPos() const;
    void move(int _row, int _col);
    void move(QPoint _pos);

    QString text() const;
    void setText(const QString &_text, Type _type = QChess::UnKonw, bool _isRed = false);

    bool isRed();

    void setSelected(bool bOk);
    bool isSelected();

    QChess::Type getType();
    void setType(QChess::Type _type);

    bool getRiver() const;
    void setRiver(bool river);

private:
    int m_row;
    int m_col;
    QString m_text;
    Type m_type;
    bool m_isRed;
    QRect m_rect;
    bool m_selected;
    bool m_river;       // 过河兵
};

#endif // QCHESSBOARD_H
